package dao.impl;

import dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;
import util.MyBatisUtil;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll() {
        return MyBatisUtil.getSession().getMapper(UserDao.class).findAll();
    }


    @Override
    public int count() {
        return 0;
    }


    @Override
    public int addUser(User user) {
        SqlSession session = MyBatisUtil.getSession();
        int result = session.getMapper(UserDao.class).addUser(user);
        //提交事务
        session.commit();
        return result;
    }

    @Override
    public int deleteUser(int id) {
        SqlSession session = MyBatisUtil.getSession();
        int result = session.getMapper(UserDao.class).deleteUser(id);
        //提交事务
        session.commit();
        return result;
    }

    @Override
    public User findUserById(int id) {
        SqlSession session = MyBatisUtil.getSession();
        User result = session.getMapper(UserDao.class).findUserById(id);
        //提交事务
        session.commit();
        return result;
    }

    @Override
    public int updateUser(User user) {
        SqlSession session = MyBatisUtil.getSession();
        int result = session.getMapper(UserDao.class).updateUser(user);
        //提交事务
        session.commit();
        return result;
    }

}
